<?php


namespace Admin\Controller;

class GoodsattrController extends AdminController {

    public function index(){
        $goodsattr = D('Goodsattr ar');

        $infos = $goodsattr->field('ar.id,ar.attr_name,ty.name,ar.rank,group_concat(val_name SEPARATOR \'/\') as val_name')->group('ar.id')->join('LEFT JOIN fanlei_goodsatval av ON ar.id=av.aid ')->join('LEFT JOIN fanlei_goodstype ty ON ar.type_id=ty.id ')->order('ar.rank')->select();
//        $infos =  $this->lists($goodsattr->field('ar.id,ar.attr_name,ty.name,ar.rank,group_concat(val_name SEPARATOR \'/\') as val_name')->group('ar.id')->join('LEFT JOIN fanlei_goodsatval av ON ar.id=av.aid ')->join('LEFT JOIN fanlei_goodstype ty ON ar.sid=ty.id ')->order('ar.rank'),array(),null);
//        dump($infos);

        $this->assign("infos",$infos);
        $this->display();
    }


    public function add(){
        $goodsattr = D('Goodsattr');
        $goodsatval = D('Goodsatval');
        $goodstype = D('Goodstype');

        if(IS_POST){
//            dump(I('attr_name'));
            $aname =trim(I('attr_name'));
            $vname =trim(I('val_name'));

            $za = $goodsattr->field('id,attr_name')->where("attr_name = '$aname'")->find();
            $zb = $goodsatval->field('id,val_name')->where("val_name = '$vname'")->find();
            if(empty($za)){
                //属性不存在
                $z1 = $goodsattr->toadd(I());
                $_POST['aid'] = $z1;
            }else{
                $_POST['aid'] = $za['id'];
            }

            if(empty($zb)){
                //属性值不存在
                $z2 = $goodsatval->toadd($_POST);
            }else{
                $this->error('添加失败,属性值已存在', U('add'));
            }


            if($z2){
                $this->success('添加成功', U('index'));
            }else{
                $this->error('更新失败', U('add'));
            }

        }else{
            $menus = $goodstype->field(true)->select();
            $menus = D('Common/Tree')->toFormatTree($menus);
//            dump($menus);
            $menus = array_merge(array(0=>array('id'=>0,'name'=>'顶级菜单')), $menus);
            $this->assign('Menus', $menus);
            $this->display();
        }

    }

    //更新
    public function upd(){
        $goodsattr = D('Goodsattr');
        $goodsatval = D('Goodsatval');
        $goodstype = D('Goodstype');

        if(IS_POST){
            $z = $goodsattr->toupd(I());
            if($z){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失败', U('upd'));
            }

        }else{
            $info = $goodsattr->find($_GET['id']);

            //分类
            $menus = $goodstype->field(true)->select();
            $menus = D('Common/Tree')->toFormatTree($menus);
//            dump($menus);
            $menus = array_merge(array(0=>array('id'=>0,'name'=>'顶级菜单')), $menus);

            $this->assign('info',$info);
            $this->assign('Menus', $menus);
            $this->display();
        }

    }

    public function del($id = null){
        $goodsattr = D('Goodsattr');

        $z =  $goodsattr->todel($id);
        if($z){
            $this->success('删除成功', U('index'));
        }else{
            $this->error('删除失败', U('index'));
        }
    }

    //排序
    public function setrank(){

//        dump(I('rank'));
        $goodsattr = D('Goodsattr');

        $z = $goodsattr->rank(I('id'),I('rank'));

        $this->ajaxReturn($z);


    }



}
